{
 "cells": [
  {
   "cell_type": "code",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "# @Author   : liyi\n",
    "# @Time     : 2022/7/3 22:07\n",
    "# @File     : sina_data.py\n",
    "# @Project  : ai_quant_trade\n",
    "# Copyright (c) Personal 2022 liyi\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "[{'day': '2022-07-01 11:30:00',\n  'open': '3393.901',\n  'high': '3404.052',\n  'low': '3391.792',\n  'close': '3392.388',\n  'volume': '6605302500',\n  'ma_price5': 3399.449,\n  'ma_volume5': 9008761980,\n  'ma_price10': 3389.963,\n  'ma_volume10': 10431126520,\n  'ma_price30': 3355.706,\n  'ma_volume30': 10238384577},\n {'day': '2022-07-01 14:00:00',\n  'open': '3392.240',\n  'high': '3392.671',\n  'low': '3379.063',\n  'close': '3384.077',\n  'volume': '6003280200',\n  'ma_price5': 3395.116,\n  'ma_volume5': 8804123340,\n  'ma_price10': 3389.357,\n  'ma_volume10': 9060893290,\n  'ma_price30': 3358.377,\n  'ma_volume30': 9940450267},\n {'day': '2022-07-01 15:00:00',\n  'open': '3384.210',\n  'high': '3389.276',\n  'low': '3379.775',\n  'close': '3387.637',\n  'volume': '6895385400',\n  'ma_price5': 3391.341,\n  'ma_volume5': 8671106900,\n  'ma_price10': 3389.795,\n  'ma_volume10': 9011547580,\n  'ma_price30': 3361.423,\n  'ma_volume30': 9925982120}]"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from urllib import request\n",
    "import json\n",
    "import pandas as pd\n",
    "\n",
    "stock_id = 'sh000001' # request data，中国平安: 601318.SH\n",
    "time_interval = 60  # 5, 15, 30, 60 min supported\n",
    "data_num = 3  # 1023 is the max one\n",
    "\n",
    "# get url path\n",
    "url_path = 'http://quotes.sina.cn/cn/api/json_v2.php/CN_MarketDataService.' \\\n",
    "      'getKLineData?symbol={0}&scale={1}&datalen={2}'.\\\n",
    "    format(stock_id, time_interval, data_num)\n",
    "\n",
    "req = request.Request(url_path)  # send request\n",
    "rsp = request.urlopen(req)  # get response\n",
    "res = rsp.read()  # get result\n",
    "res_json = json.loads(res) # parse to json\n",
    "res_json"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}